package com.xiaotu.spider.maoyan.pipeline;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;

import com.xiaotu.common.model.MaoYanMovieBoxDetailModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.APipeline;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

/**
 * @类名 MaoYanBoxOfficeDetailPipeline
 * @日期 2020年3月3日
 * @作者 wyl
 * @功能  票房明细pipeline
 */
@Component("MaoYanBoxOfficeDetailPipeline")
public class MaoYanBoxOfficeDetailPipeline extends APipeline
{
    @Override
    protected void doProcess(ResultItems resultItems, Task task){
    	 List<MaoYanMovieBoxDetailModel> boxDetailList = resultItems.get(MaoYanMovieBoxDetailModel.class.getName());
         if (boxDetailList != null && !boxDetailList.isEmpty())
         {
             String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_BOX_DETAIL_YYYYMMDD);
             cacheHandler.hMSet(key, boxDetailList.stream().collect(Collectors.toMap(mov -> mov
                     .getDataid() + SepratorUtil.SEP_UNDERLINE + mov.getPlayDate() + SepratorUtil
                     .SEP_UNDERLINE + mov.getBroadCastType() +"_"+ mov.getId(), mov -> mov)));
             cacheHandler.setCacheTimeout(key);
         }

    }

}